(:
  A procedure to number all items in a Display file
  It suppresses any previous numbering
:)
declare boundary-space strip;

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
let $t := $arguments[1]
let $horizontal := $arguments[2]
let $vertical := $arguments[3]
let $items := doc($t)/display/item
let $horizontalnodes := for $info in $items/info[@name=$horizontal] return jj:get-text($info)
let $verticalnodes := for $info in $items/info[@name=$vertical] return jj:get-text($info)
let $horizontals := fn:distinct-values($horizontalnodes)
let $verticals := fn:distinct-values($verticalnodes)
return
<display>{
<item>{
    <info name="0"></info>,
    for $hv at $p in $horizontals
    return
    <info name="{$p}">{$hv}</info>,
    <info name="total">Total</info>
}</item>,
for $vv in $verticals
return
  <item>{
    <info name="0">{$vv}</info>,
    for $hv at $p in $horizontals
    return
    <info name="{$p}">{
    	fn:count($items[jj:get-text(./info[@name=$horizontal])=$hv][jj:get-text(./info[@name=$vertical])=$vv])
    }</info>,
    <info name="total">{
      fn:count($items[jj:get-text(./info[@name=$vertical])=$vv])
    }</info>
  }</item>,
<item>{
    <info name="0">Total</info>,
    for $hv at $p in $horizontals
    return
    <info name="{$p}">{
    fn:count($items[jj:get-text(./info[@name=$horizontal])=$hv])}</info>,
    <info name="total">{fn:count($items)}</info>
}</item>  
}</display>
